package com.ysstech.system.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.entity.Dicvalue;
import com.ysstech.common.entity.Menu;
import com.ysstech.common.entity.TreeNode;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.system.enums.SystemEnum;
import com.ysstech.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 字典信息前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2021-01-11
 */
@Controller
@Slf4j
@Api(tags = "菜单信息管理api")
@RequestMapping("/menu")
public class MenuController extends BaseController {
    @Autowired
    private MenuService menuService;

    /**
     * 跳转菜单信息页面
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:menu"})
    @ApiOperation(value = "跳转菜单管理页面")
    @YssLog(module = "菜单管理", remark = "跳转菜单管理页面")
    @RequestMapping(value = "/page")
    public String page() throws Exception {
        return "system/menu";
    }

    /**
     * 加载菜单树信息
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:menu"})
    @ApiOperation(value = "获取菜单树信息")
    @YssLog(module = "菜单管理", remark = "获取菜单树信息")
    @RequestMapping(value = "/queryMenuTree")
    @ResponseBody
    public RestResult queryOrganizationTree() throws Exception {
        //不包含按钮
        List<TreeNode> list = menuService.queryAllMenu(false);
        return ResultGenerator.getSuccessResult("获取机构信息成功！", list);
    }

    @YssPermissions(value = {"system:menu"})
    @ApiOperation(value = "菜单管理页面初始化")
    @YssLog(module = "菜单管理", remark = "菜单管理页面初始化")
    @PostMapping(value = "/init")
    @ResponseBody
    public RestResult init() throws Exception {
        Map<String, Object> map = new HashMap<>();
        //不包含按钮
        List<TreeNode> listMenu = menuService.queryAllMenu(false);
        List<Dicvalue> sysMenuType = dicValueService.getCacheDicValueList(SystemEnum.SYS_MENU_TYPE.getCode());
        map.put("listMenu", listMenu);
        map.put("sysMenuType", sysMenuType);
        return ResultGenerator.getSuccessResult("菜单管理页面初始化成功", map);
    }

    /**
     * 获取一个菜单的详细信息
     *
     * @param menuId
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:menu"})
    @ApiOperation(value = "获取菜单信息")
    @YssLog(module = "菜单管理", remark = "获取菜单信息")
    @RequestMapping(value = "/queryMenuInfo")
    @ResponseBody
    public RestResult queryMenuInfo(String menuId) throws Exception {
        return ResultGenerator.getSuccessResult("获取菜单信息成功！", menuService.getById(menuId));
    }


    /**
     * 新增菜单
     *
     * @param menu
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:menu:add"})
    @ApiOperation(value = "新增菜单信息")
    @YssLog(module = "菜单管理", remark = "新增菜单信息")
    @RequestMapping(value = "/addMenu")
    @ResponseBody
    public RestResult addMenu(Menu menu) throws Exception {
        menu.setId(DateUtil.generateUUID());
        menu.setAddTime(LocalDateTime.now());
        return ResultGenerator.getSuccessResult("新增菜单信息成功！", menuService.save(menu));
    }

    /**
     * 修改菜单
     *
     * @param menu
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"system:menu:edit"})
    @ApiOperation(value = "修改菜单信息")
    @YssLog(module = "菜单管理", remark = "修改菜单信息")
    @RequestMapping(value = "/editMenu")
    @ResponseBody
    public RestResult editMenu(Menu menu) throws Exception {
        return ResultGenerator.getSuccessResult("修改菜单信息成功！", menuService.updateById(menu));
    }


    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @YssPermissions(value = {"system:menu:del"})
    @ApiOperation(value = "删除菜单信息")
    @YssLog(module = "菜单管理", remark = "删除菜单信息")
    @PostMapping(value = "/delMenu")
    @ResponseBody
    public RestResult delMenu(String id) throws Exception {
           //下级所有菜单，和菜单下的按钮
        menuService.deleteMenu(id);
        return ResultGenerator.getSuccessResult("删除菜单信息成功");
    }
}
